package com.diandiansong.app.ui.cart;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;

import com.diandiansong.app.R;
import com.diandiansong.app.entity.AddressInfo;
import com.diandiansong.app.ui.base.BaseAct;
import com.diandiansong.app.utils.A;
import com.diandiansong.app.utils.Api;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import diandiansong.library.ui.base.FastListFragment;
import diandiansong.library.utils.CCallBack;

/**
 * 选择收货地址
 */
public class AddressChoose extends BaseAct {

    public static AddressInfo.Data mAddress;

    public static boolean onActivityResultAddressChoose(int requestCode, int resultCode, Intent data) {
        return (requestCode == 1212);
    }

    public static void start(Activity context) {
        mAddress = null;
        Intent starter = new Intent(context, AddressChoose.class);
        context.startActivityForResult(starter, 1212);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addFragment(new SubFragment());

        initTitle("选择地址");
    }

    public static class SubFragment extends FastListFragment {

        int mCheckPosition = -1;

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            EventBus.getDefault().register(this);
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            EventBus.getDefault().unregister(this);
        }

        @Override
        public void initView(View rootView) {
            super.initView(rootView);
            rootView.findViewById(R.id.go_add).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AddressEdit.startAdd(getActivity());
                }
            });
        }

        @Override
        protected int getLayoutId() {
            return R.layout.address_list;
        }

        @Override
        public int getItemLayoutRes() {
            return R.layout.address_item;
        }

        @Override
        public void bindItemView(int position, @NonNull View convertView) {
            final AddressInfo.Data item = (AddressInfo.Data) getItem(position);

            fastText(convertView, R.id.tv_name, item.getNames() + "  " + item.getMobile());
            fastText(convertView, R.id.tv_address, item.getArea() + item.getAddr());

            CheckBox cb = findViewById(convertView, R.id.cb_cb);
            cb.setChecked(position == mCheckPosition);

            fastOnClick(convertView, R.id.go_edit, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AddressEdit.startEdit(getActivity(), item);
                }
            });
        }

        /**
         * 滑到底部时调用
         */
        @Override
        protected void loadMore() {
            A.address(mPage, new CCallBack<AddressInfo>() {
                @Override
                public void onSuccess(AddressInfo o) throws Exception {
                    List<AddressInfo.Data> data = o.getData();
                    if (mPage.equals("0")) {
                        mAdapter.setData(data);
                    } else {
                        mAdapter.addAll(data);
                    }
                    if (mAdapter.getDataSize() > 0) {
                        mPage = ((AddressInfo.Data) getLast()).getId();
                    }
                    onLoadFinishHasMore(data.size() >= Api.PAGE_SIZE);

                    List<AddressInfo.Data> all = mAdapter.getData();
                    if (mCheckPosition == -1) {
                        for (int i = 0; i < all.size(); i++) {
                            AddressInfo.Data item = all.get(i);
                            if (item.getIsDefault()) {
                                mCheckPosition = i;
                                mAddress = item;
                                notifyDataSetChanged();
                            }
                        }
                    }
                }

                @Override
                public void onFailure(int code, String msg) {
                    onLoadFinishNoMore();
                    toast(msg);
                }
            });
        }


        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            super.onItemClick(parent, view, position, id);
            mAddress = (AddressInfo.Data) getItem(position);
            mCheckPosition = position;
            notifyDataSetChanged();
        }

        public static class Refresh {
        }

        public static void doRefreshX() {
            EventBus.getDefault().post(new Refresh());
        }

        @Subscribe(threadMode = ThreadMode.MAIN)
        public void receiveRefresh(Refresh r) {
            onRefresh();
        }
    }
}
